<?php
namespace app\admin\controller;

use app\admin\Controller;
use think\Db;
use think\Session;
use think\Config;

class Down extends Controller{
    public function index() {
        if( isset($_POST['datetime']) && strtotime($_POST['datetime']) ) {
            $datetime = date('Ymd',strtotime($_POST['datetime']));
            $has = Db::table('__WX_BILL_LOG__')->where(['bill_date'=>$datetime])->find();
            if( $has ) {
                return ajax_return_adv_error('已经下载过账单');
            }
            $log = [
                'bill_date'=>$datetime,
                'bill_type'=>'ALL',
                'create_time'=>time(),
                'admin_id'=>Session::get(Config::get('rbac.user_auth_key'))
            ];
            $data = Db::table('__WEIXIN__')->field('appid,mchid,key,appsecret')->find();
            vendor('wxpay.native');
            $config = new \native();
            $config->setConfig([
                'APPID'=>$data['appid'],
                'MCHID'=>$data['mchid'],
                'KEY'=>$data['key'],
                'APPSECRET'=>$data['appsecret'],
            ]);
            $bill = $config->down(['bill_date'=>$datetime,'bill_type'=>'ALL']);
            if( "" == $bill ) {
                $log['note'] = '没有账单';
                Db::table('__WX_BILL_LOG__')->insert($log);
                return ajax_return_adv_error('没有账单');
            }
            $log['note'] = '账单下载成功';
            $bill = explode("\n",$bill);//根据`分割成数组
            $temp = [];
            foreach ($bill as $k=>$v){
                if( 0 < $k ) {
                    $arr = explode(',',str_replace("`","",$v));
                    if( 24 === count($arr,0) ) {
                        $temp[] = [
                            'tradetime'     =>$arr[0],//交易时间
                            'ghid'          =>$arr[1],//公众账号ID
                            'mchid'         =>$arr[2],//商户号
                            'submch'        =>$arr[3],//子商户号
                            'deviceid'      =>$arr[4], //#设备号
                            'wxorder'       =>$arr[5],//微信订单号
                            'bzorder'       =>$arr[6],//商户订单号
                            'openid'        =>$arr[7],//用户标识
                            'tradetype'     =>$arr[8],//交易类型
                            'tradestatus'   =>$arr[8],//交易状态
                            'bank'          =>$arr[10],//付款银行
                            'currency'      =>$arr[11],//货币种类
                            'totalmoney'    =>$arr[12],//总金额
                            'redpacketmoney'=>$arr[13],//企业红包总金额
                            'productname'   =>$arr[14],//商品名称
                            'bzdatapacket'    =>$arr[15],//商户数据包
                            'fee'           =>$arr[16],//手续费
                            'rate'          =>$arr[17],//费率
                        ];
                    }
                }
            }
            Db::table('__WX_BILL__')->insertAll($temp);
            Db::table('__WX_BILL_LOG__')->insert($log);
            return ajax_return_adv('下载成功');
        }
        // 模板赋值显示
        return $this->view->fetch();
    }

}
